var express = require('express');
const Result = require('../lib/Result');
const isRule = require('../util/isRule');
const GoodsSkusCard = require('../db/models/goods/goods_skus_card');
var router = express.Router();
/**
 * 添加商品规格,140,13,6
 */
router.post("", (req, res) => {
    let token = req.auth
    let goods_id = req.body.goods_id
    let name = req.body.name
    let order = req.body.order
    let type = req.body.type
    if (isRule(token.rule, [140, 13, 6], res, token.super)) {
        GoodsSkusCard.addGoodsSkusCard(goods_id, name, order, type).then(value => {
            res.json(Result.success(value.toJSON()))
        })
    }
})
/**
 * 排序商品规格，141,13,6
 */
router.post("/sort",(req,res)=>{
    let token = req.auth
    let sortdata=req.body.sortdata
    if (isRule(token.rule, [141, 13, 6], res, token.super)){
        GoodsSkusCard.sortOrder(sortdata).then(res.json(Result.success(sortdata)))
    }
})
/**
 * 选择设置商品规格选项和值,147,13,6
 */
router.post("/:id/set", (req, res) => {
    let token = req.auth

    if (isRule(token.rule, [147, 13, 6], res, token.super)) {
        GoodsSkusCard.setGoodsSkusCard(req.params.id,req.body.name,req.body.value).then(value=>{
            res.json(Result.success(value))
        })
    }
})
/**
 * 删除商品规格,143,13,6
 */
router.post("/:id/delete", (req, res) => {
    let token = req.auth

    if (isRule(token.rule, [143, 13, 6], res, token.super)) {
        GoodsSkusCard.deleteGoodsSkusCard(req.params.id).then(
            res.json(Result.success(true))
        )
    }
})
/**
 * 修改商品规格,142,13,6
 */
router.post("/:id", (req, res) => {
    let token = req.auth
    let goods_id = req.body.goods_id
    let name = req.body.name
    let order = req.body.order
    let type = req.body.type
    if (isRule(token.rule, [142, 13, 6], res, token.super)) {
        GoodsSkusCard.updateGoodsSkusCard(req.params.id, goods_id, name, order, type).then(value => {
            res.json(Result.success(value.toJSON()))
        })
    }
})
module.exports = router